<?php

class Machines extends Table
{       
    // Ecrit le tableau des machines
    public static function Tableau_Mod($db, $mod_id)//--------------------------
    {
        $sql='SELECT cli_id, cli_nom, mac_serie, mac_commission, mac_annee, mac_id, mod_id'.
            ' FROM machines'.
            ' JOIN clients ON machines.mac_cli_id = clients.cli_id'.
            ' JOIN modeles ON modeles.mod_id = machines.mac_mod_id'.
            " WHERE mod_id =$mod_id";
        $champs = array ('cli_nom', 'mac_serie', 'mac_commission', 'mac_annee', '');
        $msg_vide = LNG_MAC_VIDE;
		return parent::Tableau($db, $sql, $champs, $champs, $msg_vide, true);
    }//------------------------------------------------------------------Tableau
    
    
    // Ecrit le tableau des machines
    public static function Tableau_Cli($db, $cli_id)//-------------------------------------------------
    {
        $sql='SELECT mod_id, mod_code, mac_serie, mac_commission, mac_annee, mac_id, cli_id, cli_nom'.
            ' FROM machines'.
            ' JOIN clients ON machines.mac_cli_id = clients.cli_id'.
            ' JOIN modeles ON modeles.mod_id = machines.mac_mod_id'.
            " WHERE cli_id =$cli_id";
        $champs = array ('mod_code', 'mac_serie', 'mac_commission', 'mac_annee', '');
        $msg_vide = LNG_MAC_VIDE;
		return parent::Tableau($db, $sql, $champs, $champs, $msg_vide, true);
    }//------------------------------------------------------------------Tableau

    // Ecrit le tableau des machines
    public static function Tableau_rapide($db, $critere)//-------------------------------------------------
    {
        $sql='SELECT mod_id, mod_code, mac_serie, mac_commission, mac_annee, mac_id, cli_id, cli_nom'.
            ' FROM machines'.
            ' JOIN clients ON machines.mac_cli_id = clients.cli_id'.
            ' JOIN modeles ON modeles.mod_id = machines.mac_mod_id'.
            " WHERE mac_serie LIKE '%$critere%'".
            ' ORDER BY cli_nom';
        $champs = array ('cli_nom', 'mod_code', 'mac_serie', 'mac_commission', 'mac_annee', '');
        $msg_vide = LNG_MAC_VIDE;
		return parent::Tableau($db, $sql, $champs, $msg_vide);
    }//------------------------------------------------------------------Tableau


	public static function Tableau($db, $mac_mod_id=-1, $mac_serie='')
	{
		function moinsunversrien($var)
		{
			return ($var==-1)?'':$var;
		}
		$mac_mod_id = moinsunversrien($mac_mod_id);
		$where = Array();
		if ( !empty($mac_serie) )
			$where[] = "mac_serie LIKE '%$mac_serie%'";
		if ( !empty($mac_mod_id) )
			$where[] = "mac_mod_id = $mac_mod_id";
		if ( sizeof($where)>0 ) 
		{
			$where = ' WHERE '.implode(' AND ', $where);
		}
		
        $sql='SELECT mod_id, mod_code, mac_serie, mac_commission, mac_annee, mac_id, cli_id, cli_nom'.
            ' FROM machines'.
            ' JOIN clients ON machines.mac_cli_id = clients.cli_id'.
            ' JOIN modeles ON modeles.mod_id = machines.mac_mod_id'.
            $where.
            ' ORDER BY cli_nom';
        $champs = array ('cli_nom', 'mod_code', 'mac_serie', 'mac_commission', 'mac_annee', '');
        $msg_vide = LNG_MAC_VIDE;
		return parent::Tableau($db, $sql, $champs, $msg_vide);
	}


	public static function UpdateMod($db, $ancien,$nouveau){
		$sql = "UPDATE machines SET mac_mod_id='$nouveau' WHERE mac_mod_id='$ancien'";
        if ( $result = $db->query($sql) ){
			return True;
		}
		return False;
	}

}//*****************************************************************Utilisateurs


class Machine extends Enregistrement
{
	// Constructeur
	function __construct($db)//-------------------------------------------------
	{
		$this->defaut = Array(	'mac_id' 			=> -1,
								'mac_commission' 	=> '',
								'mac_serie' 		=> '',
								'mac_annee' 		=> '',
								'mac_cli_id' 		=> '',
								'mac_commentaire' 	=> '',
								'mac_mod_id' 		=> '');						
		parent::__construct($db, 'machines', 'mac_id');
	}//--------------------------------------------------------------__construct

	function Ecrire(){
		$retour = parent::Ecrire();
		if ($retour){
			$sql = "UPDATE clients SET cli_annee='".date('Y-m-d')."' WHERE cli_id='{$this->attribut['mac_cli_id']}'";
			$this->db->query($sql);
		}
		return $retour;
	}

	// Verification des attributs via la base
	function Verification()//---------------------------------------------------
	{

	}//-------------------------------------------------------------Verification

	function NomFormat()
	{
		return $this->attribut['mac_serie'];
	}

	function Traitement(){
		if ( isset( $_POST['maj_sup'] ) )
		{
			if ( isset( $_POST['confirmation'] ) )
				if ( $this->Supprimer() )
				{
					$_SESSION[SES_STATUT] = LNG_STA_UTI_SUP_0;
					header('location:climachines.php?id='.+$this->attribut['mac_cli_id']);
				}
				else
					$_SESSION[SES_STATUT] = LNG_STA_ERREUR.LNG_STA_UTI_SUP_1;
			else
				$erreur['confirmation'] = LNG_UTI_CONFIRMATION_AIDE;
			return;
		}
		//return parent::Traitement();
		if (parent::Traitement()){
			header('location:climachines.php?id='.$this->attribut['mac_cli_id']);
		}
	}


	function Contenu($erreur, $onglet=0, $edition=False)//----------------------
	{
		$h4 = LNG_H4_INFORMATIONS;
		// Affichage du contenu
		if ( droit_modifier(GR_MAC_MODIF, $edition) )
			$h4 .= Liens::MacModifier($this->cle());
		?>
		<form method='post' action='machine.php?<?="id={$this->cle()}&amp;edition=$edition"?>'>
		<?php
		dohtml_h4($h4);
		$this->ChampSelect('mac_mod_id', $edition);
		$this->ChampSelect('mac_cli_id', $edition);
		$this->ChampText('mac_serie', $edition);
		$this->ChampText('mac_commission', $edition);
		$this->ChampText('mac_annee', $edition);
		$this->ChampArea('mac_commentaire', $edition);
		if ($edition)
			dohtml_formbouton('labelagauche', 'Mettre à jour la base', 'maj_infos', 'Annuler les modifications');
		?>
		</form>
		<?php
		if ( !droit_test(GR_MAC_SUPPR, False) || $this->Cle()==-1)
			return;
		?>
		<form method='post' action='machine.php?<?="id={$this->cle()}&amp;edition=$edition"?>'>
		<?php 
		dohtml_h4(LNG_H4_SUPPRESSION);
		dohtml_formcheck('labelagauche',
						'confirmation',
						LNG_UTI_CONFIRMATION,
						LNG_UTI_CONFIRMATION_AIDE,
						$erreur['confirmation']);
		dohtml_formbouton('labelagauche', 'Supprimer de la base', 'maj_sup');
		?>
		</form>
		<?php		
	}
}
?>
